

//
//  FirstController.m
//  UI12-QQ-xib
//
//  Created by 韩旭 on 16/7/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
#import "Masonry.h"
#import "FirstController.h"
#import "HX_CustomView.h"
#import "SliderView.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "AudioPlayerManager.h"
#import "CALayer+PauseAimate.h"
@interface FirstController ()<AVAudioPlayerDelegate, AudioPlayerManagerDelegate>
@property (nonatomic, strong) HX_CustomView *hx_custom;
@property (nonatomic, strong) AVPlayer *avplayer;

@property (nonatomic, strong) AVAudioPlayer *player;
@property (nonatomic, strong) SliderView *sliderView;
@property (nonatomic, strong) AudioPlayerManager *audioPlayerManger;
@property (nonatomic, assign) BOOL isBofang;
@end

@implementation FirstController

+ (FirstController *)shareDetailViewController {
    static FirstController *first = nil;
    static dispatch_once_t once;
  dispatch_once(&once, ^{
      first = [[FirstController alloc] init];
  });
    return first;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.audioPlayerManger = [AudioPlayerManager shareAudioPlayerManager];
        self.audioPlayerManger.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];

//    NSURL *url = [NSURL URLWithString:@"http://sc1.111ttt.com/2016/1/08/20/201201356449.mp3"];
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    self.avplayer = [[AVPlayer alloc] initWithPlayerItem:item];
    [self loadWebNet];
    [self setBlurView];
  self.hx_custom = [[[NSBundle mainBundle] loadNibNamed:@"HX_CustomView" owner:self options:nil] firstObject];
    
    __weak FirstController *first = self;
    
    [self addIncoViewImage];
  [_hx_custom playBlockPlayerBtn:^(UIButton *btnPlayer) {
      [self.audioPlayerManger play];
      [first.sliderView addPlayerTimer];
      [self.hx_custom.playImage.layer resumeAnimate];
  } StopBtn:^(UIButton *btnStopBtn) {
       [first.audioPlayerManger pause];
      [self.audioPlayerManger pause];
      [self.hx_custom.playImage.layer pauseAnimate];
  }];
    self.sliderView = [[SliderView alloc] init];
    [self.view addSubview:_sliderView];
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(10);
        make.height.mas_equalTo(40);
        make.right.equalTo(self.view).with.offset(-10);
        make.bottom.equalTo(self.view).with.offset(-170);
    }];
    [self.sliderView changeSliderValue:^(CGFloat value) {
        [first.sliderView removePlayerTimer];
        [first.audioPlayerManger pause];
        [first.audioPlayerManger seekToTime:CMTimeMakeWithSeconds(value, first.audioPlayerManger.currentTime.timescale) completionHandler:^(BOOL finished) {
            if (finalStage) {
                [first.audioPlayerManger play];
                [first.sliderView addPlayerTimer];
            }
        }];
    }];
    [self.sliderView playerSliderValue:^(UISlider *slider) {
    float value = CMTimeGetSeconds(self.audioPlayerManger.currentItem.currentTime);
        slider.value = value;
    [first.sliderView currentValue:value];
    }];
    
    [self.sliderView sliderThumb:@"player_slider_playback_thumb" maximumTrackImage:@"player_slider_playback_right" minimumTrackImage:@"player_slider_playback_left"];
   __weak typeof (self) weakSelf = self;
    [self.audioPlayerManger addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 30) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float total = CMTimeGetSeconds(weakSelf.audioPlayerManger.item.duration);
        [first.sliderView musicOverallValue:total];
    }];
}
- (void)loadWebNet {
    [self.audioPlayerManger playerPlayWithUrl:@"http://audio.xmcdn.com/group14/M06/55/F3/wKgDZFW87eeieeWPAKWKIhpBNk8680.mp3"];
    [self.audioPlayerManger pause];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addIncoViewImage {
    CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    ba.fromValue = @(0);
    ba.toValue = @(M_PI * 2);
    ba.repeatCount = MAXFLOAT;
    ba.duration = 2;
    [self.hx_custom.playImage.layer addAnimation:ba forKey:nil];
    [self.hx_custom.playImage.layer pauseAnimate];
}

- (void)setBlurView {
    UIToolbar *bar = [[UIToolbar alloc] init];
    bar.barStyle = UIBarStyleBlack;
    bar.frame = self.view.bounds;
    [self.hx_custom.image addSubview:bar];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
